package com.njtoyo.taxi.admin.logic.business.ride;

import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.njtoyo.taxi.admin.mapper.master.entity.Ride;
import com.njtoyo.taxi.admin.mapper.master.entity.RideCancellation;
import com.njtoyo.taxi.admin.mapper.master.enums.RideCancellationResponsibility;
import com.njtoyo.taxi.admin.mapper.master.enums.RideCancellationTriggerBy;
import com.njtoyo.taxi.admin.mapper.master.enums.RideCancellationViolate;
import com.njtoyo.taxi.admin.mapper.master.mapper.RideCancellationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class RideCancellationLogic {

    @Autowired
    private RideCancellationMapper rideCancellationMapper;

    /**
     * 有责改无责，操作RideCancellation
     */
    public boolean cancellationResponsibilityEliminate(Long rideCancellationId) {
        return new LambdaUpdateChainWrapper<>(rideCancellationMapper)
                .eq(RideCancellation::getId, rideCancellationId)
                .set(RideCancellation::getViolate, RideCancellationViolate.none)
                .set(RideCancellation::getResponsibility, RideCancellationResponsibility.admin)
                .set(RideCancellation::getTriggerBy, RideCancellationTriggerBy.admin)
                .set(RideCancellation::getUpdatedAt, new Date())
                .update();
    }

    /**
     * 新增行程取消记录
     * 强制取消订单+管理员直接取消行程时，根据 ride 新增一条记录
     */
    public boolean insertRecordForCancel(Ride ride) {
        RideCancellation rideCancellation = new RideCancellation();
        rideCancellation.setRideId(ride.getId());
        rideCancellation.setUserId(ride.getUserId());
        rideCancellation.setDriverId(ride.getDriverId());
        rideCancellation.setTriggerBy(RideCancellationTriggerBy.admin);
        rideCancellation.setResponsibility(RideCancellationResponsibility.admin);
        rideCancellation.setViolate(RideCancellationViolate.none);

        return rideCancellationMapper.insert(rideCancellation) > 0;
    }
}
